/*
 * BPlusTreeDefinitions.cpp
 *
 *  Created on: Nov 7, 2012
 *      Author: m
 */

#ifndef BPLUSTREEDEFINITIONS_CPP_
#define BPLUSTREEDEFINITIONS_CPP_

#include "BPlusTreeDefinitions.h"
#include "LeafNodeSerializer.h"
#include "InternalNodeSerializer.h"

#define ORDER 1000

std::string UpToLow( std::string str )
{
    for ( unsigned int i = 0; i < str.size(); i++ )
        if ( str[i] >= 0x41 && str[i] <= 0x5A )
            str[i] = str[i] + 0x20;
    return str;
}

DTRkey_t cropKey_func( const DTRkey_t& key, const DTRkey_t& prevKey )
{
    unsigned int i;
    for ( i = 0; ( i < prevKey.size() && prevKey[i] == key[i] ); i++ )
        ;
    return key.substr( 0, i + 1 );
}

bool keyComparator_func( const DTRkey_t& key1, const DTRkey_t& key2 )
{
    //return UpToLow( key1 ) < UpToLow( key2 );
    return key1 < key2;
}

bool isOverflowLeaf_func( const LeafRegisterList_t& list )
{
    return ( list.size() > ORDER );
}

bool isUnderflowLeaf_func( const LeafRegisterList_t& list )
{
    return ( list.size() <= ( ORDER / 2 + ORDER % 2 ) );
}

bool isOverflowInternal_func( const InternalRegisterList_t& list )
{
    return ( list.size() > ORDER * 2 );
}

bool isUnderflowInternal_func( const InternalRegisterList_t& list )
{
    return ( list.size() <= ( ORDER + ORDER % 2 ) );
}

size_t getLeafNodeSize( const Leaf* leaf )
{
    LeafNodeSerializer leafNodeSerializer;

    size_t returnValue = leafNodeSerializer.computeSize( leaf->getSerializationBlock() );

    return returnValue;
}

size_t getInternalNodeSize( const Internal* internal )
{
    InternalNodeSerializer internalNodeSerializer;

    return internalNodeSerializer.computeSize( internal->getSerializationBlock() );
}

#endif /* BPLUSTREEDEFINITIONS_CPP_ */
